package com.zyg.shop.service.impl;

import com.alibaba.fastjson.JSON;
import com.zyg.shop.entity.*;
import com.zyg.shop.entity.group.Goods;
import com.zyg.shop.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyg.common.utils.PageUtils;
import com.zyg.common.utils.Query;

import com.zyg.shop.dao.GoodsDao;
import org.springframework.transaction.annotation.Transactional;


@Service("goodsService")
@Transactional
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {


    @Autowired
    private GoodsDescService goodsDescService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private ItemCatService itemCatService;
    @Autowired
    private ItemService itemService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 作者：LMY
     * 功能：添加商品各种数据到不同表中
     * 时间：2021/10/31 21:01
     */
    @Override
    public void save(Goods goods) {
        //1.1 添加spu商品(tb_goods)
        GoodsEntity goodsEntity = goods.getGoods();
        goodsEntity.setAuditStatus("0");        //未审核
        goodsEntity.setSellerId("baidu");       //商家id
        baseMapper.insert(goodsEntity);

        //1.2 添加spu商品描述(tb_goodsDesc)
        GoodsDescEntity goodsDesc = goods.getGoodsDesc();
        goodsDesc.setGoodsId(goodsEntity.getId());
        goodsDescService.save(goodsDesc);

        //1.3 添加sku商品列表(tb_items)
        insertItems(goods);
    }


    /**
     * 作者：LMY
     * 功能：添加sku商品列表
     * 时间：2021/10/31 21:15
     */
    private void insertItems(Goods goods) {
        for (ItemEntity item : goods.getItems()) {
            //2.1 设置外键字段
            item.setGoodsId(goods.getGoods().getId());
            BrandEntity brand = brandService.getById(goods.getGoods().getBrandId());
            //2.2 设置品牌名
            item.setBrand(brand.getName());
            //2.3 设置商家名称
            GoodsEntity goodsEntity = goods.getGoods();
            String sellerId = goodsEntity.getSellerId();
            SellerEntity sellerEntity = sellerService.getById(sellerId);

            item.setSeller(sellerEntity.getSellerId());
            //2.4 设置标题

            String goodsName = goodsEntity.getGoodsName();
            item.setTitle(goodsName);
            //2.5 设置图像
            GoodsDescEntity goodsDesc = goodsDescService.getById(goods.getGoods().getId());
            String itemImages = goodsDesc.getItemImages();
            List<Map> maps = JSON.parseArray(itemImages, Map.class);
            Object url = maps.get(0).get("url");
            item.setImage(url + "");
            //2.6 设置创建及修改时间及分类名称
            item.setCreateTime(new Date());
            item.setUpdateTime(new Date());
            item.setCategory(itemCatService.getById(goods.getGoods().getCategory3Id()).getName());
            //2.7 设置状态值
            item.setStatus("0");
            item.setCategoryid(goods.getGoods().getCategory3Id());
            //2.8 添加sku商品
            itemService.save(item);
        }
    }

    /**
     * 作者：LMY
     * 功能：修改前查询，数据回显
     * 时间：2021/10/31 23:21
     */
    @Override
    public Goods findGoodsById(Long id) {
        //1 查询出spu商品
        GoodsEntity goodsEntity = baseMapper.selectById(id);
        //2. 查询出商品描述
        GoodsDescEntity goodsDescEntity = goodsDescService.getById(id);
        //3. 查询出sku商品列表
        List<ItemEntity> items = itemService.list(new QueryWrapper<ItemEntity>().eq("goods_id", id));
        //4. 返回携带数据的goods对象
        return new Goods(goodsEntity,goodsDescEntity,items);
    }
    /**
     * 作者：LMY
     * 功能：修改
     */
    public void updateGoods(Goods goods) {
        //1. 修改spu商品
        baseMapper.updateById(goods.getGoods());
        //2. 修改商品描述
        goodsDescService.updateById(goods.getGoodsDesc());
        //3. 先删除原有的sku商品列表
        itemService.remove(new QueryWrapper<ItemEntity>().eq("goods_id",goods.getGoods().getId()));
        //4. 重新添加
        insertItems(goods);
    }

}